package com.radon.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.radon.service.RabbitMQService;
import com.radon.util.Constant;
import com.radon.websocket.DemoWebSocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

/**
 * RabbitMQ的service层，用于封装发送消息的方法
 */
@Service
public class RabbitMQServiceImpl implements RabbitMQService {
    private static Logger logger = LoggerFactory.getLogger(RabbitMQServiceImpl.class);

    @Override
    @RabbitListener(queues = "direct.queue",errorHandler = "RabbitConsumerListenerErrorHandler")
    @RabbitHandler // 此注解加上之后可以接受对象型消息
    public void direct(@Payload JSONObject jsonObject, Channel channel,@Headers Map<String, Object> heads) throws IOException {
        try {
            DemoWebSocket.BroadCastInfo("消息id:"+heads.get("id")+"内容"+jsonObject.toJSONString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        //logger.info("消息id:"+heads.get("id")+"内容"+jsonObject.toJSONString());

        long deTag = (Long) heads.get(AmqpHeaders.DELIVERY_TAG);
        System.out.println(deTag);
        // 告诉服务器，已经消费成功,
        channel.basicAck(deTag, false);

    }

    @RabbitListener(queues = "direct.queue")
    @RabbitHandler // 此注解加上之后可以接受对象型消息
    public void direct2(@Payload JSONObject jsonObject, Channel channel,@Headers Map<String, Object> heads) throws IOException {
        /*try {
            DemoWebSocket.BroadCastInfo("消息id:"+heads.get("id")+"内容"+jsonObject.toJSONString());
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        logger.info("消息id:"+heads.get("id")+"内容"+jsonObject.toJSONString());

        long deTag = (Long) heads.get(AmqpHeaders.DELIVERY_TAG);
        System.out.println(deTag);
        // 告诉服务器，已经消费成功,
        channel.basicAck(deTag, false);

    }

    @RabbitListener(queues = "fanout.queue")
    @RabbitHandler // 此注解加上之后可以接受对象型消息
    public void fanout(@Payload JSONObject jsonObject, Channel channel,@Headers Map<String, Object> heads) throws IOException {
        /*try {
            DemoWebSocket.BroadCastInfo("消息id:"+heads.get("id")+"内容"+jsonObject.toJSONString());
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        logger.info("1消息id:"+heads.get("id")+"内容"+jsonObject.toJSONString());

        long deTag = (Long) heads.get(AmqpHeaders.DELIVERY_TAG);
        System.out.println(deTag);
        // 告诉服务器，已经消费成功,
        channel.basicAck(deTag, false);

    }

    @RabbitListener(queues = "fanout.queue.two")
    @RabbitHandler // 此注解加上之后可以接受对象型消息
    public void fanout2(@Payload JSONObject jsonObject, Channel channel,@Headers Map<String, Object> heads) throws IOException {
        /*try {
            DemoWebSocket.BroadCastInfo("消息id:"+heads.get("id")+"内容"+jsonObject.toJSONString());
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        logger.info("2消息id:"+heads.get("id")+"内容"+jsonObject.toJSONString());

        long deTag = (Long) heads.get(AmqpHeaders.DELIVERY_TAG);
        System.out.println(deTag);
        // 告诉服务器，已经消费成功,
        channel.basicAck(deTag, false);

    }

    @RabbitListener(queues = "topic.queue")
    @RabbitHandler // 此注解加上之后可以接受对象型消息
    public void topic(@Payload JSONObject jsonObject, Channel channel,@Headers Map<String, Object> heads) throws IOException {
        /*try {
            DemoWebSocket.BroadCastInfo("消息id:"+heads.get("id")+"内容"+jsonObject.toJSONString());
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        logger.info("1消息id:"+heads.get("id")+"内容"+jsonObject.toJSONString());

        long deTag = (Long) heads.get(AmqpHeaders.DELIVERY_TAG);
        System.out.println(deTag);
        // 告诉服务器，已经消费成功,
        channel.basicAck(deTag, false);

    }
    @RabbitListener(queues = "topic.queue.two")
    @RabbitHandler // 此注解加上之后可以接受对象型消息
    public void topic2(@Payload JSONObject jsonObject, Channel channel,@Headers Map<String, Object> heads) throws IOException {
        /*try {
            DemoWebSocket.BroadCastInfo("消息id:"+heads.get("id")+"内容"+jsonObject.toJSONString());
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        logger.info("2消息id:"+heads.get("id")+"内容"+jsonObject.toJSONString());

        long deTag = (Long) heads.get(AmqpHeaders.DELIVERY_TAG);
        System.out.println(deTag);
        // 告诉服务器，已经消费成功,
        channel.basicAck(deTag, false);

    }
}
